package news.yp.newsproject.view.activity;

import android.content.Intent;
import android.net.Uri;
import android.os.Environment;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.ProgressBar;
import android.widget.TextView;
import android.widget.Toast;

import com.folioreader.FolioReader;
import com.news.yp.baselibrary.annotation.ViewById;
import com.news.yp.baselibrary.annotation.ViewClick;
import com.news.yp.baselibrary.base.BaseActivityPresenter;

import java.io.File;

import news.yp.newsproject.R;
import news.yp.newsproject.bean.ResponseDetailData;
import news.yp.newsproject.network.ApiServiceFactor;
import news.yp.newsproject.presenter.presenter.CenterPresenter;
import news.yp.newsproject.presenter.presenter.DetailPresenter;
import news.yp.newsproject.utils.DownLoadUtils;

/**
 * 准备好，我们要开始看书了
 */
public class ShouBookActivity extends BaseActivityPresenter<DetailPresenter> implements CenterPresenter.DetailViewPresenter {
    @ViewById(R.id.click)
    Button button;

    @ViewById(R.id.progress)
    ProgressBar progressBar;
    @ViewById(R.id.progress_value)
    TextView textValue;
    @ViewById(R.id.zhifubao)
    Button mButton;

    private String bookpath;
    private String filePath;

    @Override
    protected DetailPresenter createPresenter() {
        return new DetailPresenter();
    }

    @Override
    protected void setContentView() {
        setContentView(R.layout.shoubook_layout);
    }

    @Override
    protected void initView() {

    }


    @Override
    protected void initTitle() {

    }

    @Override
    protected void initData() {
        bookpath = getIntent().getStringExtra("bookpath");
        //  /home/yp666/book/482/mobi8/2.一人食.epub
        Log.e("TAG", "详情界面传过来的书：" + bookpath);
        filePath = "";

    }

    @Override
    public void onLoading() {

    }

    @Override
    public void onError() {

    }

    @Override
    public void onDetailSuccess(ResponseDetailData responseDetailData) {

    }

    @ViewClick({R.id.click, R.id.zhifubao})
    public void click(View view) {
        switch (view.getId()) {
            case R.id.click:
                progressBar.setVisibility(View.VISIBLE);
                //   File file = new File(Environment.getExternalStorageDirectory().getAbsolutePath() + "/bookpdf/HTML_CSS_JavaScript网页制作从入门到精通》作者_ 刘西杰.epub");
                //   startOpen(file);

                DownLoadUtils.create(this).startDownload(ApiServiceFactor.bookurl + "getpdf/" + bookpath, Environment.getExternalStorageDirectory().getAbsolutePath() + "/bookpdf", new DownLoadUtils.DownLoadListener() {
                    @Override
                    public void onSuccess(File file) {
                        Log.e("TAG", "下载成功--" + file.getAbsolutePath());
                        //startOpen(file);
                        runOnUiThread(new Runnable() {
                            @Override
                            public void run() {
                                Toast.makeText(ShouBookActivity.this, "本书已经存储到 boopdf 文件夹下面，快去看看吧～～", Toast.LENGTH_SHORT).show();
                            }
                        });
                    }

                    @Override
                    public void onFail(Exception e) {
                        Log.e("TAG", "文件下载失败" + e.getMessage().toString());
                    }

                    @Override
                    public void onProgress(final int progress) {
                        runOnUiThread(new Runnable() {
                            @Override
                            public void run() {
                                progressBar.setProgress(progress);
                                textValue.setText(String.valueOf(progress));
                            }
                        });

                        Log.e("TAG", "下载进度-----" + progress);
                    }

                });
                break;
            case R.id.zhifubao:
                String qrcode = "HTTPS://QR.ALIPAY.COM/FKX01681RTXYXEXI3ON86F?t=1544108898994";
                String alipayqr = "alipayqr://platformapi/startapp?saId=10000007&clientVersion=3.7.0.0718&qrcode=" + qrcode;
                openUri(alipayqr + "%3F_s%3Dweb-other&_t=" + System.currentTimeMillis());
                break;
        }
    }

    private void startOpen(File file) {
        FolioReader folioReader = FolioReader.get();
        folioReader.openBook(file.getAbsolutePath());
    }


    private void openUri(String s) {
        Log.e("TAG", "----支付宝");
        Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(s));
        ShouBookActivity.this.startActivity(intent);
    }

}
